package com.smart.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.smart.dao.MaintenanceManInfoDAO;
import com.smart.dao.RepairRequestDAO;
import com.smart.model.MaintenanceManInfo;
import com.smart.model.RepairRequest;
import com.smart.model.RepairRequestBriefInfo;

public class RepairRequestService {
	static RepairRequestDAO repairRequestDAO = null;
	static MaintenanceManInfoDAO maintenanceManInfoDAO = null;

	static {
		repairRequestDAO = new RepairRequestDAO();
		maintenanceManInfoDAO = new MaintenanceManInfoDAO();
	}

	public boolean addRepairRequest(RepairRequest repairRequest) throws SQLException {
		return repairRequestDAO.addRepairRequest(repairRequest);
	}

	public boolean judgeExitenceOfUnhandledRepairRequest() throws SQLException {
		return repairRequestDAO.judgeExitenceOfUnhandledRepairRequest();
	}
	
	public List<RepairRequest> getRepairRequestList(int userID, int status, int page, int pageSize) throws SQLException {
		return repairRequestDAO.getRepairRequestList(userID, status, page, pageSize);
	}

	public HashMap<String, Object> getRepairRequestList(int status, int page) throws SQLException {
		return repairRequestDAO.getRepairRequestList(status, page);
	}

	public HashMap<String, Object> getRepairRequestList(int status, int page, String dateFrom, String dateTo)
			throws SQLException {
		return repairRequestDAO.getRepairRequestList(status, page, dateFrom, dateTo);
	}

	public RepairRequestBriefInfo getRepairRequestDetailsByID(int id) throws SQLException {
		return repairRequestDAO.getRepairRequestDetailByID(id);
	}

	public List<MaintenanceManInfo> getMaintenanceManInfoList() throws SQLException {
		return maintenanceManInfoDAO.getMaintenanceManInfoList();
	}

	public void updateRepairRequestRecord(int newStatus, int recordID, int maintenanceManID) throws SQLException {
		switch (newStatus) {
		case 1:
			// 处理报修请求
			repairRequestDAO.updateRepairRequestRecord(newStatus, recordID, maintenanceManID);
			maintenanceManInfoDAO.dispatchTask(maintenanceManID);
			// 向用户微信号发送消息
			break;
		case 2:
			// 完成报修
			repairRequestDAO.updateRepairRequestRecord(newStatus, recordID);
			maintenanceManInfoDAO.finishTask(maintenanceManID);
			break;
		}
	}

	public RepairRequestBriefInfo getRepairRequestBriefInfoByID(int recordID) throws SQLException {
		return repairRequestDAO.getRepairRequestBriefInfoByID(recordID);
	}
	
	public int getUserIDByOpenID(String openID) throws SQLException {
		return repairRequestDAO.getUserIDByOpenID(openID);
	}
}
